From 8d6703f300b325309a2ebd3ed0a275395e7aa2de Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Sat, 2 Mar 2024 16:53:37 -0600 Subject: [PATCH] Use the new attrs API on SQLCommand classes --- src/pgwui_core/core.py | 55 +++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/src/pgwui_core/core.py b/src/pgwui_core/core.py index 3f6c91a..206ecc2 100644 --- a/src/pgwui_core/core.py +++ b/src/pgwui_core/core.py @@ -42,6 +42,7 @@ from csv import reader as csv_reader import collections.abc import ast import attr +import attrs import markupsafe import hashlib @@ -652,26 +653,21 @@ def format_exception(ex): # Upload processing -class SQLCommand(object): +@attrs.define(slots=False) +class SQLCommand(): ''' An SQL command that returns nothing Attributes: stmt The statement, formatted for psycopg3 substitution args Tuple of arguments used to substitute when executed. + ec(ex) Produces the exception to raise an instance of on failure + Input: + ex The exception raised by psycopg3 ''' - def __init__(self, stmt, args, ec=None): - ''' - stmt The statement, formatted for psycopg3 substitution - args Tuple of arguments used to substitute when executed. - ec(ex) Produces the exception to raise an instance of on failure - Input: - ex The exception raised by psycopg3 - ''' - super(SQLCommand, self).__init__() - self.stmt = stmt - self.args = args - self.ec = ec + stmt = attrs.field() + args = attrs.field() + ec = attrs.field(default=None) def execute(self, cur): ''' @@ -693,20 +689,25 @@ class SQLCommand(object): raise self.ec(ex) +@attrs.define(slots=False) class LogSQLCommand(SQLCommand): - '''An SQL command that logs success or failure.''' - def __init__(self, stmt, args, ec=None, - log_success=None, log_failure=None): - ''' - stmt The statement, formatted for psycopg3 substitution - args Tuple of arguments used to substitute when executed. - ec(ex) Produces the exception to raise an instance of on failure - Input: - ex The exception raised by psycopg3 - ''' - super(LogSQLCommand, self).__init__(stmt, args, ec) - self.log_success = log_success - self.log_failure = log_failure + '''An SQL command that logs success or failure. + + Attributes: + stmt The statement, formatted for psycopg3 substitution + args Tuple of arguments used to substitute when executed. + ec(ex) Produces the exception to raise an instance of on failure + Input: + ex The exception raised by psycopg3 + log_success + Logs success + log_failure(ex) + Logs failure + Input: + ex The exception to log + ''' + log_success = attrs.field(default=None) + log_failure = attrs.field(default=None) def execute(self, cur): ''' @@ -720,7 +721,7 @@ class LogSQLCommand(SQLCommand): Can raise a psycopg3 error ''' try: - super(LogSQLCommand, self).execute(cur) + super().execute(cur) except (core_ex.UploadError, psycopg.DatabaseError) as ex: if self.log_failure: self.log_failure(ex) -- 2.34.1